home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / mail / pine3.96.tar.gz / pine3.96.tar / pine3.96 / imap / ANSI / c-client / strtol.c < prev    next >
Text File  |  1993-11-16  |  4KB  |  110 lines

  1. /*
  2.  * Program:    String to long
  3.  *
  4.  * Author:    Mark Crispin
  5.  *        Networks and Distributed Computing
  6.  *        Computing & Communications
  7.  *        University of Washington
  8.  *        Administration Building, AG-44
  9.  *        Seattle, WA  98195
  10.  *
  11.  * Date:    11 May 1989
  12.  * Last Edited:    12 November 1993
  13.  *
  14.  * Copyright 1993 by the University of Washington
  15.  *
  16.  *  Permission to use, copy, modify, and distribute this software and its
  17.  * documentation for any purpose and without fee is hereby granted, provided
  18.  * that the above copyright notice appears in all copies and that both the
  19.  * above copyright notice and this permission notice appear in supporting
  20.  * documentation, and that the name of the University of Washington not be
  21.  * used in advertising or publicity pertaining to distribution of the software
  22.  * without specific, written prior permission.  This software is made
  23.  * available "as is", and
  24.  * THE UNIVERSITY OF WASHINGTON DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED,
  25.  * WITH REGARD TO THIS SOFTWARE, INCLUDING WITHOUT LIMITATION ALL IMPLIED
  26.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, AND IN
  27.  * NO EVENT SHALL THE UNIVERSITY OF WASHINGTON BE LIABLE FOR ANY SPECIAL,
  28.  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  29.  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, TORT
  30.  * (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF OR IN CONNECTION
  31.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  32.  *
  33.  */
  34.  
  35. /*
  36.  * Turn a string long into the real thing (swiped from KCC)
  37.  * Accepts: source string
  38.  *        pointer to place to return end pointer
  39.  *        base
  40.  * Returns: parsed long integer, end pointer is updated
  41.  */
  42.  
  43. long strtol (char *s,char **endp,int base)
  44. {
  45.   long value = 0;        /* the accumulated value */
  46.   int negative = 0;        /* this a negative number? */
  47.   int ok;            /* true while valid chars for number */
  48.   char c;
  49.                 /* insist upon valid base */
  50.   if (base && (base < 2 || base > 36)) return NIL;
  51.   while (isspace (*s)) s++;    /* skip leading whitespace */
  52.   if (!base) {            /* if base = 0, */
  53.     if (*s == '-') {        /* integer constants are allowed a */
  54.       negative = 1;        /* leading unary minus, but not */
  55.       s++;            /* unary plus. */
  56.     }
  57.     else negative = 0;
  58.     if (*s == '0') {        /* if it starts with 0, its either */
  59.                 /* 0X, which marks a hex constant, */
  60.       if (toupper (*++s) == 'X') {
  61.     s++;            /* skip the X */
  62.            base = 16;        /* base is hex 16 */
  63.       }
  64.       else base = 8;        /* leading 0 means octal number */
  65.     }
  66.     else base = 10;        /* otherwise, decimal. */
  67.     do {
  68.       switch (base) {        /* char has to be valid for base */
  69.       case 8:            /* this digit ok? */
  70.     ok = isodigit(*s);
  71.     break;
  72.       case 10:
  73.     ok = isdigit(*s);
  74.     break;
  75.       case 16:
  76.     ok = isxdigit(*s);
  77.     break;
  78.       default:            /* it's good form you know */
  79.     return NIL;
  80.       }
  81.                 /* if valid char, accumulate */
  82.       if (ok) value = value * base + toint(*s++);
  83.     } while (ok);
  84.     if (toupper(*s) == 'L') s++;/* ignore 'l' or 'L' marker */
  85.     if (endp) *endp = s;    /* set user pointer to after num */
  86.     return (negative) ? -value : value;
  87.   }
  88.  
  89.   switch (*s) {            /* check for leading sign char */
  90.   case '-':
  91.     negative = 1;        /* yes, negative #.  fall into '+' */
  92.   case '+':
  93.     s++;            /* skip the sign character */
  94.   }
  95.                 /* allow hex prefix "0x" */
  96.   if (base == 16 && *s == '0' && toupper (*(s + 1)) == 'X') s += 2;
  97.   do {
  98.                 /* convert to numeric form if digit*/
  99.     if (isdigit (*s)) c = toint (*s);
  100.                 /* alphabetic conversion */
  101.     else if (isalpha (*s)) c = *s - (isupper (*s) ? 'A' : 'a') + 10;
  102.     else break;            /* else no way it's valid */
  103.     if (c >= base) break;    /* digit out of range for base? */
  104.     value = value * base + c;    /* accumulate the digit */
  105.   } while (*++s);        /* loop until non-numeric character */
  106.   if (endp) *endp = s;        /* save users endp to after number */
  107.                 /* negate number if needed */
  108.   return (negative) ? -value : value;
  109. }
  110.